<?php

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Coupons;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class CouponsAdminController extends Controller
{

    protected $pageNum=20;

    public function login()
    {
        return view( 'coupons_admin/login' );
    }


    public function doLogin(Request $request)
    {

        $account  = $request->account;
        $password = md5($request->password );
        $where=[
            'account'=>$account,
            "password"=>$password,
            "type"=>6

        ];
        $user=Admin::where($where)->first();

        if(!empty(collect($user)->toArray())){
            $Session      = Session();
            $Session->put( "coupons_add",$user['id'] );
            return Redirect::to( 'Admin/couponsAdmin/index' );
        }else{
            return back()->with( 'error','用户名或密码错误' );
        }

    }



    public function index(){
        $Session      = Session();
        $coupons = $Session->get( 'coupons_add' );
        if (empty( $coupons )){
            return Redirect::to( 'Admin/couponsAdmin/login' )->with(["meg"=>"登陆过期，请重新登陆"]);
        }
        $coupon=Coupons::orderBy("id","desc")->paginate($this->pageNum);
        $checkCoupons=collect($coupon)->toArray();
        if(!empty($checkCoupons['data'])){
            $now=time();
            foreach ($checkCoupons['data'] as $key =>$value){
                $end=strtotime($value['closed_at']);
                if($end<$now){
                    $where=[
                        'id'=> $value[ 'id' ],
                    ];
                    $param=[
                        'state'=>5
                    ];
                    $res = DB::table('coupons')->where($where)->update($param);
                }
            }
        }
        $coupons=Coupons::orderBy("id","desc")->paginate($this->pageNum);
        $data=[
            "coupons"
        ];
        return view("coupons_admin/index",compact($data));
    }



    public  function addCoupons(Request $request){
        date_default_timezone_set('PRC');
        $creat_time     = date("Y-m-d H:i:s", time());
        $param = [
            'coupons_name' => $request[ 'name' ],
            "created_at"     => $creat_time,
            "coupons_type"   => $request[ 'type' ],
            'coupons_state'  => 1,
            'coupons_amount' => $request[ 'amount' ],
            'coupons_num'    => $request[ 'number' ],
            'started_at'     => $request[ 'started_at' ],
            'closed_at'      => $request[ 'closed_at' ],
            "remain_num"     => 0,
        ];
        $res = DB::table('coupons')->insert($param);
        if (!$res) {  //上传成功
            return back()->with('error', '添加失败');
        } else { //插入数据库失败
            return back()->with('success', '添加成功');
        }

    }


    public  function submitAudit(Request $request){
        $id=$request->id;
        $where=[
            "id"=>$id
        ];
        $param=[
            "coupons_state"=>2
        ];
        $res = DB::table('coupons')->where($where)->update($param);
        if (!$res) {  //上传成功
            return back()->with('error', '提交审核失败');
        } else { //插入数据库失败
            return back()->with('success', '提交审核成功');
        }
    }


    public  function editCoupons(Request $request){
        $param = [
            'coupons_name'   => $request[ 'coupons_name' ],
            "coupons_type"   => $request[ 'coupons_type' ],
            'coupons_amount' => $request[ 'coupons_amount' ],
            'coupons_num'    => $request[ 'coupons_number' ],
            'started_at'     => $request[ 'coupons_started_at' ],
            'closed_at'      => $request[ 'coupons_closed_at' ],
        ];
        $where=[
            'id'=> $request[ 'id' ],
        ];
        $res = DB::table('coupons')->where($where)->update($param);
        if (!$res) {  //上传成功
            return back()->with('error', '修改失败');
        } else { //插入数据库失败
            return back()->with('success', '修改成功');
        }

    }

    public function loginOut(){

        $Session      = Session();
        $coupons = $Session->remove( 'coupons_add' );

        return Redirect::to( 'Admin/couponsAdmin/login' );

    }



    public function delete(){



    }



}
